package com.booter.mvel;

import org.mvel2.UnresolveablePropertyException;
import org.mvel2.integration.VariableResolver;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.integration.impl.MapVariableResolver;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lcw
 * @date 2022/8/1 10:15
 */
public class CustomMapVariableResolverFactory extends CustomBaseVariableResolverFactory {
    private static final long serialVersionUID = 1493440221272270805L;
    protected Map<String, Object> variables;

    public CustomMapVariableResolverFactory() {
        this.variables = new ConcurrentHashMap<>();
    }

    public CustomMapVariableResolverFactory(Map<String, Object> variables) {
        this.variables = variables;
    }

    public CustomMapVariableResolverFactory(Map<String, Object> variables, VariableResolverFactory nextFactory) {
        this.variables = variables;
        this.nextFactory = nextFactory;
    }

    public CustomMapVariableResolverFactory(Map<String, Object> variables, boolean cachingSafe) {
        this.variables = variables;
    }

    /**
     * 根据函数名称删除对应的函数构建
     *
     * @param fun 函数名称
     */
    public void remove(String fun) {
        this.variables.remove(fun);
        variableResolvers.remove(fun);
    }

    /**
     * 使用新的函数构建替换原有的函数
     *
     * @param fun       函数名称
     * @param variables 变量
     * @param resolver  resolver
     */
    public void replace(String fun, Object variables, VariableResolver resolver) {
        synchronized (fun) {
            this.variables.put(fun, variables);
            variableResolvers.put(fun, resolver);
        }
    }

    public Object getVariables(String fun) {
        return variables.get(fun);
    }


    @Override
    public VariableResolver createVariable(String name, Object value) {
        VariableResolver vr;
        try {
            (vr = getVariableResolver(name)).setValue(value);
            return vr;
        } catch (UnresolveablePropertyException e) {
            addResolver(name, vr = new MapVariableResolver(variables, name)).setValue(value);
            return vr;
        }
    }

    @Override
    public VariableResolver createVariable(String name, Object value, Class<?> type) {
        VariableResolver vr;
        try {
            vr = getVariableResolver(name);
        } catch (UnresolveablePropertyException e) {
            vr = null;
        }
        if (vr != null && vr.getType() != null) {
            throw new RuntimeException("variable already defined within scope: " + vr.getType() + " " + name);
        } else {
            addResolver(name, vr = new MapVariableResolver(variables, name, type)).setValue(value);
            return vr;
        }
    }

    @Override
    public VariableResolver getVariableResolver(String name) {
        VariableResolver vr = variableResolvers.get(name);
        if (vr != null) {
            return vr;
        } else if (variables.containsKey(name)) {
            variableResolvers.put(name, vr = new MapVariableResolver(variables, name));
            return vr;
        } else if (nextFactory != null) {
            return nextFactory.getVariableResolver(name);
        }
        throw new UnresolveablePropertyException("unable to resolve variable '" + name + "'");
    }


    @Override
    public boolean isResolveable(String name) {
        return (variableResolvers.containsKey(name))
                || (variables != null && variables.containsKey(name))
                || (nextFactory != null && nextFactory.isResolveable(name));
    }

    protected VariableResolver addResolver(String name, VariableResolver vr) {
        variableResolvers.put(name, vr);
        return vr;
    }


    @Override
    public boolean isTarget(String name) {
        return variableResolvers.containsKey(name);
    }

    @Override
    public Set<String> getKnownVariables() {
        if (variables != null) {
            return new HashSet<>(variables.keySet());
        }
        return new HashSet<>(0);
    }

    public void clear() {
        variableResolvers.clear();
        variables.clear();
    }

}
